package com.xprime.mobileFrameworkIOS
{
    import flash.events.EventDispatcher;
    import flash.events.StatusEvent;
    import flash.external.ExtensionContext;

    /**
     * Simple NativeAlert extension that allows you to
     * Open device specific alerts and recieve information about
     * what button the user pressed to close the alert.
     */
    public class AneInterfaceIOS extends EventDispatcher
    {
        //---------------------------------------------------------------------
        //
        // Constants
        //
        //---------------------------------------------------------------------

        private static const EXTENSION_ID : String = "com.xprime.IOSane";

        //---------------------------------------------------------------------
        //
        // Private Properties.
        //
        //---------------------------------------------------------------------

        private var context : ExtensionContext;

        //---------------------------------------------------------------------
        //
        // Public Methods.
        //
        //---------------------------------------------------------------------

        public function AneInterfaceIOS()
        {
            context = ExtensionContext.createExtensionContext( EXTENSION_ID, null );
			context.addEventListener( StatusEvent.STATUS, onContextEventHandler );
        }

        /**
         * @param title Title to be displayed in the Alert.
         * @param message Message to be displayed in the Alert.
         * @param closeLabel Label for the close button.
         * @param otherLabels shoud be a comma separated sting of button labels.
         * for example "one,two,three"
         */
        public function alert( title : String, message : String,
            closeLabel : String, otherLabels : String = "" ) : void
        {
            
            context.call( "showAlertWithTitleAndMessage", title, message, closeLabel, otherLabels );
        }
		
		
		/**
		 * vibration
		 */		
		public function vibrate():void
		{
			context.call( "vibrateDevice" );
		}
		
		
		/**
		 * vibration
		 */		
		public function isVibrationSupported():Boolean
		{
			return context.call( "isVibrationSupported" ) as Boolean;
		}
		
		
		/**
		 * loading
		 */		
		public function loading( isLoadingON : Boolean = true , ...args ):void
		{
			if( isLoadingON )
			{
				context.call( "loadingON" );
			}
			else
			{
				context.call( "loadingOFF" );
			}
		}
		
		
		
		/**
		 * file download
		 */		
		public function fileDownload(  saveFileName: String , saveFilePath : String, url : String ):void
		{
			context.call( "fileDownload", url, saveFileName, saveFilePath );
		}
		
		
		/**
		 * file open
		 */		
		public function fileOpen( fileName: String, path : String ):void
		{
			context.call( "fileOpen", path, fileName);
		}
		

		/**
		 * dispose
		 */		
		public function dispose():void
		{
			this.context.dispose();
		}

        //---------------------------------------------------------------------
        //
        // Private Methods.
        //
        //---------------------------------------------------------------------

        /**
         * @private
         * When the Alert closes in the device we will redispatch the event
         * through ActionScript so that we can handle the user interaction.
         * The event will contain the index of the button that the user selected.
         */
        private function onContextEventHandler( event : StatusEvent ) : void
        {
			dispatchEvent( event );
        }
    }
}
